package com.horse.three60;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.horse.utils.Constants;
import com.horse.utils.DownloadingItem;
import com.horse.utils.ImageName;
import com.horse.utils.Utils;
import com.inapp.util.BillingService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PurchaseActivity extends Activity implements OnClickListener,
		OnTouchListener {

	private ImageView bundleButton;
	private ImageView muscleButton;
	private ImageView hoofButton;
	private ImageView deepMuscleButton;
	private ImageView headButton;
	private ImageView organButton;
	private Button doneButton;

	private ImageView imageView;

	private TextView titleView;
	private TextView descriptionView;

	private TextView priceView;
	private ImageView buyButton;

	private String text = "Welcome to horse 360's Store.Here you'll be able to view and purchase new layeers that"
			+ " unlock new levels.Additional sets will be also added into Vet Talk and Q50 Challenge sections\n\n"
			+ "Choose from any of the tiles below:";

	private int level;
	private String imageFileName[];

	private String IMAGE_DIRECTORY;
	private String BASE_URL = "";
	private String ACHIVEMENT_ID = "";

	private String BUNDLE_TEXT = "The whole package including the superficial muscles,hooves,organs,"
			+ "conformation,deep mucles,joints,ligaments,head and teeth can all be purchased in this one "
			+ "bundle.A total of 17 levels added";
	private String SUPER_TEXT = "Get all superficial muscle levels,increase the jumble quize options and "
			+ "more Vet talk Layers.This app will teach you what good horsemen know...improve your "
			+ "knowledge about how your horse works";
	private String HOOF_TEXT = "No Hoof No Horse- All horseman know that you need to take care of your horses feet."
			+ "Know everything"
			+ " about the most important part of this great animal. Talk on the same level as your "
			+ "farrier or vet and work to improve health and performance";
	private String DEEP_TEXT = "Get a deeper knowledge of horses by"
			+ " learning about the deep muscle,joint and ligament layers.This will also add more questions to the "
			+ "jumble quiz and also unlock new layers in Vet Talk";
	private String HEAD_TEXT = "Take a bite of knowledge and see what a horse's head is made of.Learn about the "
			+ "bone and muscle layers of the head and also the set of teeth it has. Unlock 3 crunshing levels";
	private String ORGAN_TEXT = "Get inside knowledge of your horse and learn what makes "
			+ "tick. Beautifully rendered images in high definition. Unlock for new levels as well as adding more "
			+ "layers to learn in the Vet Talk section";

	private String BUNDLE_TITLE = "The Total Horse 360 Package";
	private String SUPER_TITLE = "Muscle Pack of 2 levels";
	private String HOOF_TITLE = "Hoof Pack of 4 levels";
	private String DEEP_TITLE = "Deep Muscles,Joints & Ligaments Pack of 4 Levels";
	private String HEAD_TITLE = "Head & Teeth Pack of 3 levels";
	private String ORGAN_TITLE = "Organs & Conformation Pack of 4 levels";

	private String title;
	private String description;
	private String priceText;

	private boolean isMainBundle;

	private String itemId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_purchase_screen);

			try {
				sharedPreferences = PreferenceManager
						.getDefaultSharedPreferences(this);
				sharedPreferences
						.registerOnSharedPreferenceChangeListener(ospcl);
			} catch (Exception e) {
				e.printStackTrace();
			}

			doneButton = (Button) findViewById(R.id.doneButton);
			doneButton.setOnClickListener(this);

			imageView = (ImageView) findViewById(R.id.imageView);

			priceView = (TextView) findViewById(R.id.priceView);

			buyButton = (ImageView) findViewById(R.id.buyButton);
			buyButton.setOnClickListener(this);
			buyButton.setOnTouchListener(this);

			titleView = (TextView) findViewById(R.id.titleView);

			descriptionView = (TextView) findViewById(R.id.descriptionView);
			descriptionView.setText(text);

			bundleButton = (ImageView) findViewById(R.id.bundleButton);
			bundleButton.setOnClickListener(this);
			bundleButton.setOnTouchListener(this);

			muscleButton = (ImageView) findViewById(R.id.muscleButton);
			muscleButton.setOnClickListener(this);
			muscleButton.setOnTouchListener(this);

			hoofButton = (ImageView) findViewById(R.id.hoofButton);
			hoofButton.setOnClickListener(this);
			hoofButton.setOnTouchListener(this);

			deepMuscleButton = (ImageView) findViewById(R.id.deepMucleButton);
			deepMuscleButton.setOnClickListener(this);
			deepMuscleButton.setOnTouchListener(this);

			headButton = (ImageView) findViewById(R.id.headButton);
			headButton.setOnClickListener(this);
			headButton.setOnTouchListener(this);

			organButton = (ImageView) findViewById(R.id.organButton);
			organButton.setOnClickListener(this);

			if (!Utils.isDeviceOnline(this)) {
				Utils.showAlertMessage(this,
						android.R.drawable.ic_dialog_alert,
						"Connection error ! !",
						"This package requires an internet connection to complete");
			} else {
				priceView.setVisibility(View.VISIBLE);
				priceView.setText("$1.99");
				buyButton.setVisibility(View.VISIBLE);
			}

			level = getIntent().getIntExtra(Constants.KEY_LEVEL, 3);
			levelWiseOperation(level);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private SharedPreferences sharedPreferences;
	private final OnSharedPreferenceChangeListener ospcl = new OnSharedPreferenceChangeListener() {

		public void onSharedPreferenceChanged(final SharedPreferences sp,
				String key) {
			try {
				if (BillingService.KEY_IN_APP_PURCHASED_ITEM_CHANGED_TIME
						.equals(key)) {
					runOnUiThread(new Runnable() {
						public void run() {
							try {
								if (BillingService.isPurchased(
										sharedPreferences, itemId)) {
									if (!isMainBundle) {
										downloadImage();
									} else {
										downloadAllImages();
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	@Override
	protected void onDestroy() {
		try {
			if (sharedPreferences != null && ospcl != null) {
				sharedPreferences
						.unregisterOnSharedPreferenceChangeListener(ospcl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

	private void levelWiseOperation(int level) {
		switch (level) {
		case 3:
		case 4:
			isMainBundle = false;
			priceText = "$1.99";
			title = SUPER_TITLE;
			description = SUPER_TEXT;
			IMAGE_DIRECTORY = Constants.MUSLES_DIRECTORY;
			imageFileName = ImageName.MUSCLES_IMAGES;
			BASE_URL = Constants.MUSCLES_STANDARD_URL;
			imageView.setImageResource(R.drawable.main_muscle);
			itemId = Constants.MUSCLE_ITEM_ID;
			ACHIVEMENT_ID = Constants.MUSCLE_THERAPIST_ACHIEVEMENT_ID;
			break;

		case 5:
		case 6:
		case 7:
		case 8:
			isMainBundle = false;
			priceText = "$1.99";
			title = HOOF_TITLE;
			description = HOOF_TEXT;
			IMAGE_DIRECTORY = Constants.HOOFS_DIRECTORY;
			imageFileName = ImageName.HOOFS_IMAGES;
			BASE_URL = Constants.HOOVES_RETINAL_URL;
			imageView.setImageResource(R.drawable.main_hoof);
			itemId = Constants.HOOF_ITEM_ID;
			ACHIVEMENT_ID = Constants.HORSE_DENTIST_ACHIEVEMENT_ID;
			break;

		case 9:
		case 10:
		case 11:
		case 12:
			isMainBundle = false;
			priceText = "$1.99";
			title = DEEP_TITLE;
			description = DEEP_TEXT;
			BASE_URL = Constants.TENDONS_STANDARD_URL;
			IMAGE_DIRECTORY = Constants.DEEP_MUSCLES_DIRECTORY;
			imageFileName = ImageName.TENDON_IMAGES;
			imageView.setImageResource(R.drawable.main_deep_muscle);
			itemId = Constants.DEEP_ITEM_ID;
			ACHIVEMENT_ID = Constants.PRO_RIDER_WORLD_CHAMPION_ACHIEVEMENT_ID;
			break;

		case 13:
		case 14:
		case 15:
			isMainBundle = false;
			priceText = "$1.99";
			title = HEAD_TITLE;
			description = HEAD_TEXT;
			BASE_URL = Constants.HEAD_STANDARD_URL;
			IMAGE_DIRECTORY = Constants.HEAD_DIRECTORY;
			imageFileName = ImageName.HEAD_IMAGES;
			imageView.setImageResource(R.drawable.main_head);
			itemId = Constants.HEAD_ITEM_ID;
			ACHIVEMENT_ID =Constants.CHIROPRACTOR_ACHIEVEMENT_ID;
			break;
		case 16:
		case 17:
		case 18:
		case 19:
			isMainBundle = false;
			priceText = "$1.99";
			title = ORGAN_TITLE;
			description = ORGAN_TEXT;
			BASE_URL = Constants.ORGANS_STANDARD_URL;
			IMAGE_DIRECTORY = Constants.ORGANS_DIRECTORY;
			imageFileName = ImageName.ORGANS_IMAGES;
			imageView.setImageResource(R.drawable.main_organ);
			itemId = Constants.ORGAN_ITEM_ID;
		    ACHIVEMENT_ID = Constants.FARRIER_ACHIEVEMENT_ID;
			break;

		default:
			isMainBundle = true;
			imageView.setImageResource(R.drawable.mainbundle);
			priceText = "$8.49";
			title = BUNDLE_TITLE;
			description = BUNDLE_TEXT;
			IMAGE_DIRECTORY = Constants.MUSLES_DIRECTORY;
			imageFileName = ImageName.MUSCLES_IMAGES;
			BASE_URL = Constants.MUSCLES_STANDARD_URL;
			itemId = Constants.BUNDLE_ITEM_ID;
			break;
		}
		titleView.setText(title);
		descriptionView.setText(description);
		priceView.setText(priceText);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bundleButton:
			levelWiseOperation(0);
			break;
		case R.id.muscleButton:
			levelWiseOperation(3);
			break;
		case R.id.hoofButton:
			levelWiseOperation(5);
			break;
		case R.id.deepMucleButton:
			levelWiseOperation(9);
			break;
		case R.id.headButton:
			levelWiseOperation(13);
			break;
		case R.id.organButton:
			levelWiseOperation(16);
			break;

		case R.id.buyButton:
			buyPopup();
			break;

		case R.id.doneButton:
			// quiz = getIntent().getIntExtra(Constants.KEY_QUIZ, 0);
			// if (quiz == Constants.QUIZ_LEARNING) {
			// Intent intent = new Intent(PurchaseActivity.this,
			// LearningBeforeActivity.class);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(intent);
			// } else {
			// Intent intent = new Intent(PurchaseActivity.this,
			// RecallBeforeActivity.class);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(intent);
			// }
			finish();
			break;

		default:
			break;
		}

	}

	private void buyPopup() {
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Confirm Your In App Purchase");
		builder.setMessage(
				"Do you want to to buy one " + title + " for " + priceText)
				.setCancelable(false)
				.setPositiveButton("Buy",
						new DialogInterface.OnClickListener() {
							public void onClick(final DialogInterface dialog,
									final int id) {
								dialog.cancel();
								// if (!isMainBundle) {
								// downloadImage();
								// } else {
								// downloadAllImages();
								// }
								HomeScreenActivity.requestPurchase(
										sharedPreferences, itemId);

							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int arg1) {
								dialog.cancel();

							}
						});

		final AlertDialog alert = builder.create();
		alert.show();
	}

	private void downloadImage() {
		try {
			
			if(IMAGE_DIRECTORY!=null && imageFileName!=null &&BASE_URL!=null){
				List<DownloadingItem> downloadingItems = new ArrayList<DownloadingItem>();
				
				if(!Utils.containsAllFiles(IMAGE_DIRECTORY,Arrays.asList(imageFileName))){
					DownloadingItem di = new DownloadingItem();	
					di.setACHIEVEMENT_ID(ACHIVEMENT_ID);
					di.setBASE_URL(BASE_URL);
					di.setIMAGE_DIRECTORY(IMAGE_DIRECTORY);
					di.setImageFileName(imageFileName);
					downloadingItems.add(di);
				}
				if(downloadingItems.size()>0){
					Utils.downloadFiles(PurchaseActivity.this,downloadingItems,null);
				}
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		try {
//			new AsyncTask<String, String, String>() {
//				private ProgressDialog progressDialog;
//				private int imageIndex = 0;
//
//				@Override
//				protected void onPreExecute() {
//					try {
//
//						progressDialog = new ProgressDialog(
//								PurchaseActivity.this);
//						progressDialog.setMessage("Downloading....");
//						progressDialog.setIndeterminate(false);
//						progressDialog.setCancelable(false);
//						progressDialog.setMax(imageFileName.length);
//						progressDialog
//								.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//
//						progressDialog.show();
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//
//				protected void onProgressUpdate(String... arg0) {
//					try {
//						progressDialog.setProgress(imageIndex);
//						if (imageIndex == imageFileName.length) {
//							Toast.makeText(PurchaseActivity.this,
//									"Download completed", Toast.LENGTH_SHORT)
//									.show();
//							switch (level) {
//							case 3:
//							case 4:
//								unlockedLevels = new int[2];
//								unlockedLevels[0] = 3;
//								unlockedLevels[1] = 4;
//								new Achievement(
//										Constants.MUSCLE_THERAPIST_ACHIEVEMENT_ID)
//										.unlock(new Achievement.UnlockCB() {
//
//											@Override
//											public void onSuccess(
//													boolean newUnlock) {
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_OK);
//												// LearningBeforeActivity.this.finish();
//
//											}
//
//											@Override
//											public void onFailure(
//													String exceptionMessage) {
//												Toast.makeText(
//														PurchaseActivity.this,
//														"Error ("
//																+ exceptionMessage
//																+ ") unlocking achievement.",
//														Toast.LENGTH_SHORT)
//														.show();
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_CANCELED);
//												// LearningBeforeActivity.this.finish();
//											}
//
//										});
//								break;
//
//							case 5:
//							case 6:
//							case 7:
//							case 8:
//								unlockedLevels = new int[4];
//								unlockedLevels[0] = 5;
//								unlockedLevels[1] = 6;
//								unlockedLevels[2] = 7;
//								unlockedLevels[3] = 8;
//								new Achievement(
//										Constants.HORSE_DENTIST_ACHIEVEMENT_ID)
//										.unlock(new Achievement.UnlockCB() {
//
//											@Override
//											public void onSuccess(
//													boolean newUnlock) {
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_OK);
//												// LearningBeforeActivity.this.finish();
//
//											}
//
//											@Override
//											public void onFailure(
//													String exceptionMessage) {
//												Toast.makeText(
//														PurchaseActivity.this,
//														"Error ("
//																+ exceptionMessage
//																+ ") unlocking achievement.",
//														Toast.LENGTH_SHORT)
//														.show();
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_CANCELED);
//												// LearningBeforeActivity.this.finish();
//											}
//
//										});
//								break;
//
//							case 9:
//							case 10:
//							case 11:
//							case 12:
//								unlockedLevels = new int[4];
//								unlockedLevels[0] = 9;
//								unlockedLevels[1] = 10;
//								unlockedLevels[2] = 11;
//								unlockedLevels[3] = 12;
//								new Achievement(
//										Constants.PRO_RIDER_WORLD_CHAMPION_ACHIEVEMENT_ID)
//										.unlock(new Achievement.UnlockCB() {
//
//											@Override
//											public void onSuccess(
//													boolean newUnlock) {
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_OK);
//												// LearningBeforeActivity.this.finish();
//
//											}
//
//											@Override
//											public void onFailure(
//													String exceptionMessage) {
//												Toast.makeText(
//														PurchaseActivity.this,
//														"Error ("
//																+ exceptionMessage
//																+ ") unlocking achievement.",
//														Toast.LENGTH_SHORT)
//														.show();
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_CANCELED);
//												// LearningBeforeActivity.this.finish();
//											}
//
//										});
//								break;
//							case 13:
//							case 14:
//							case 15:
//								unlockedLevels = new int[3];
//								unlockedLevels[0] = 13;
//								unlockedLevels[1] = 14;
//								unlockedLevels[2] = 15;
//								new Achievement(
//										Constants.CHIROPRACTOR_ACHIEVEMENT_ID)
//										.unlock(new Achievement.UnlockCB() {
//
//											@Override
//											public void onSuccess(
//													boolean newUnlock) {
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_OK);
//												// LearningBeforeActivity.this.finish();
//
//											}
//
//											@Override
//											public void onFailure(
//													String exceptionMessage) {
//												Toast.makeText(
//														PurchaseActivity.this,
//														"Error ("
//																+ exceptionMessage
//																+ ") unlocking achievement.",
//														Toast.LENGTH_SHORT)
//														.show();
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_CANCELED);
//												// LearningBeforeActivity.this.finish();
//											}
//
//										});
//								break;
//							case 16:
//							case 17:
//							case 18:
//							case 19:
//								unlockedLevels = new int[4];
//								unlockedLevels[0] = 16;
//								unlockedLevels[1] = 17;
//								unlockedLevels[2] = 18;
//								unlockedLevels[3] = 19;
//								new Achievement(
//										Constants.FARRIER_ACHIEVEMENT_ID)
//										.unlock(new Achievement.UnlockCB() {
//
//											@Override
//											public void onSuccess(
//													boolean newUnlock) {
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_OK);
//												// LearningBeforeActivity.this.finish();
//
//											}
//
//											@Override
//											public void onFailure(
//													String exceptionMessage) {
//												Toast.makeText(
//														PurchaseActivity.this,
//														"Error ("
//																+ exceptionMessage
//																+ ") unlocking achievement.",
//														Toast.LENGTH_SHORT)
//														.show();
//												PurchaseActivity.this
//														.setResult(Activity.RESULT_CANCELED);
//												// LearningBeforeActivity.this.finish();
//											}
//
//										});
//								break;
//
//							default:
//								break;
//							}
//							// for (int i = 0; i < unlockedLevels.length; i++) {
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED
//							// + unlockedLevels[i], true);
//							// }
//
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//
//				@Override
//				protected String doInBackground(String... arg0) {
//					try {
//						while (imageIndex < imageFileName.length) {
//
//							String url = BASE_URL
//									+ "/"
//									+ Utils.getAndroidFormattedUrl(imageFileName[imageIndex]);
//							Log.i("DREG", "File name = " + BASE_URL + "/"
//									+ imageFileName[imageIndex]);
//							Downloader.downloadFile(IMAGE_DIRECTORY,
//									imageFileName[imageIndex], url,
//									PurchaseActivity.this);
//							imageIndex++;
//							publishProgress("");
//						}
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//					return null;
//				}
//
//				@Override
//				protected void onPostExecute(String unused) {
//					progressDialog.dismiss();
//					// for (int i = 0; i < unlockedLevels.length; i++) {
//					// if (Utils.getLevelUnlocked(PurchaseActivity.this,
//					// Constants.KEY_LEVEL_UNLOCKED
//		// + unlockedLevels[i])) {
//		// isLevelUnlocked[unlockedLevels[i]] = true;
//		// }
//		// }
//		// LearningBeforeActivity.learnView
//		// .setLockedLevel(isLevelUnlocked);
//		// LearningBeforeActivity.learnView.invalidate();
//	}
//}.execute();
//
//} catch (Exception e) {
//e.printStackTrace();
//}
	}

	private void downloadAllImages() {
		try {
			
			List<DownloadingItem> downloadingItems = new ArrayList<DownloadingItem>();
			if(!Utils.containsAllFiles(Constants.MUSLES_DIRECTORY,Arrays.asList(ImageName.MUSCLES_IMAGES))){
				DownloadingItem di = new DownloadingItem();	
				di.setACHIEVEMENT_ID(Constants.MUSCLE_THERAPIST_ACHIEVEMENT_ID);
				di.setBASE_URL(Constants.MUSCLES_STANDARD_URL);
				di.setIMAGE_DIRECTORY(Constants.MUSLES_DIRECTORY);
				di.setImageFileName(ImageName.MUSCLES_IMAGES);
				downloadingItems.add(di);
			}
			if(!Utils.containsAllFiles(Constants.HOOFS_DIRECTORY,Arrays.asList(ImageName.HOOFS_IMAGES))){
				DownloadingItem di = new DownloadingItem();
				di.setACHIEVEMENT_ID(Constants.HORSE_DENTIST_ACHIEVEMENT_ID);	
				di.setBASE_URL(Constants.HOOVES_RETINAL_URL);
				di.setIMAGE_DIRECTORY(Constants.HOOFS_DIRECTORY);
				di.setImageFileName(ImageName.HOOFS_IMAGES);
				downloadingItems.add(di);
			}
			if(!Utils.containsAllFiles(Constants.DEEP_MUSCLES_DIRECTORY,Arrays.asList(ImageName.TENDON_IMAGES))){
				DownloadingItem di = new DownloadingItem();
				di.setACHIEVEMENT_ID(Constants.PRO_RIDER_WORLD_CHAMPION_ACHIEVEMENT_ID);
				di.setBASE_URL(Constants.TENDONS_STANDARD_URL);
				di.setIMAGE_DIRECTORY(Constants.DEEP_MUSCLES_DIRECTORY);
				di.setImageFileName(ImageName.TENDON_IMAGES);
				downloadingItems.add(di);
			}
			if(!Utils.containsAllFiles(Constants.HEAD_DIRECTORY,Arrays.asList(ImageName.HEAD_IMAGES))){
				DownloadingItem di = new DownloadingItem();
				di.setACHIEVEMENT_ID(Constants.CHIROPRACTOR_ACHIEVEMENT_ID);
				di.setBASE_URL(Constants.HEAD_STANDARD_URL);
				di.setIMAGE_DIRECTORY(Constants.HEAD_DIRECTORY);
				di.setImageFileName(ImageName.HEAD_IMAGES);
				downloadingItems.add(di);
			}
			if(!Utils.containsAllFiles(Constants.ORGANS_DIRECTORY,Arrays.asList(ImageName.ORGANS_IMAGES))){
				DownloadingItem di = new DownloadingItem();
				di.setACHIEVEMENT_ID(Constants.FARRIER_ACHIEVEMENT_ID);
				di.setBASE_URL(Constants.ORGANS_STANDARD_URL);
				di.setIMAGE_DIRECTORY(Constants.ORGANS_DIRECTORY);
				di.setImageFileName(ImageName.ORGANS_IMAGES);
				downloadingItems.add(di);
			}
			if(downloadingItems.size()>0){
				Utils.downloadFiles(PurchaseActivity.this,downloadingItems,null);
			}
			
//			
//			
//			new AsyncTask<String, String, String>() {
//				private ProgressDialog progressDialog;
//				private int imageIndex = 0;
//				private int totalFiles;
//				private int count = 0;
//
//				@Override
//				protected void onPreExecute() {
//					try {
//						totalFiles = ImageName.MUSCLES_IMAGES.length
//								+ ImageName.HOOFS_IMAGES.length
//								+ ImageName.TENDON_IMAGES.length
//								+ ImageName.HEAD_IMAGES.length
//								+ ImageName.ORGANS_IMAGES.length;
//
//						IMAGE_DIRECTORY = Constants.MUSLES_DIRECTORY;
//						imageFileName = ImageName.MUSCLES_IMAGES;
//						BASE_URL = Constants.MUSCLES_STANDARD_URL;
//
//						// unlockedLevels = new int[20];
//
//						progressDialog = new ProgressDialog(
//								PurchaseActivity.this);
//						progressDialog.setMessage("Downloading....");
//						progressDialog.setIndeterminate(false);
//						progressDialog.setCancelable(false);
//						progressDialog.setMax(totalFiles);
//						progressDialog
//								.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//
//						progressDialog.show();
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//
//				protected void onProgressUpdate(String... arg0) {
//					try {
//						progressDialog.setProgress(imageIndex);
//						count++;
//						if (imageIndex == ImageName.MUSCLES_IMAGES.length) {
//							count = 0;
//							// unlockedLevels[0] = 3;
//							// unlockedLevels[1] = 4;
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 3, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 4, true);
//
//							IMAGE_DIRECTORY = Constants.HOOFS_DIRECTORY;
//							imageFileName = ImageName.HOOFS_IMAGES;
//							BASE_URL = Constants.HOOVES_RETINAL_URL;
//						}
//						if (imageIndex == (ImageName.MUSCLES_IMAGES.length + ImageName.HOOFS_IMAGES.length)) {
//							count = 0;
//							// unlockedLevels[2] = 5;
//							// unlockedLevels[3] = 6;
//							// unlockedLevels[4] = 7;
//							// unlockedLevels[5] = 8;
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 5, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 6, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 7, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 8, true);
//							new Achievement(
//									Constants.MUSCLE_THERAPIST_ACHIEVEMENT_ID)
//									.unlock(new Achievement.UnlockCB() {
//
//										@Override
//										public void onSuccess(boolean newUnlock) {
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_OK);
//											// LearningBeforeActivity.this.finish();
//
//										}
//
//										@Override
//										public void onFailure(
//												String exceptionMessage) {
//											Toast.makeText(
//													PurchaseActivity.this,
//													"Error ("
//															+ exceptionMessage
//															+ ") unlocking achievement.",
//													Toast.LENGTH_SHORT).show();
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_CANCELED);
//											// LearningBeforeActivity.this.finish();
//										}
//
//									});
//
//							BASE_URL = Constants.TENDONS_STANDARD_URL;
//							IMAGE_DIRECTORY = Constants.DEEP_MUSCLES_DIRECTORY;
//							imageFileName = ImageName.TENDON_IMAGES;
//						}
//
//						if (imageIndex == (ImageName.MUSCLES_IMAGES.length
//								+ ImageName.HOOFS_IMAGES.length + ImageName.TENDON_IMAGES.length)) {
//							count = 0;
//							// unlockedLevels[6] = 9;
//							// unlockedLevels[7] = 10;
//							// unlockedLevels[8] = 11;
//							// unlockedLevels[9] = 12;
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 9, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 10, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 11, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 12, true);
//							new Achievement(
//									Constants.HORSE_DENTIST_ACHIEVEMENT_ID)
//									.unlock(new Achievement.UnlockCB() {
//
//										@Override
//										public void onSuccess(boolean newUnlock) {
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_OK);
//											// LearningBeforeActivity.this.finish();
//
//										}
//
//										@Override
//										public void onFailure(
//												String exceptionMessage) {
//											Toast.makeText(
//													PurchaseActivity.this,
//													"Error ("
//															+ exceptionMessage
//															+ ") unlocking achievement.",
//													Toast.LENGTH_SHORT).show();
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_CANCELED);
//											// LearningBeforeActivity.this.finish();
//										}
//
//									});
//
//							BASE_URL = Constants.HEAD_STANDARD_URL;
//							IMAGE_DIRECTORY = Constants.HEAD_DIRECTORY;
//							imageFileName = ImageName.HEAD_IMAGES;
//							imageView.setImageResource(R.drawable.main_head);
//
//						}
//						if (imageIndex == (ImageName.MUSCLES_IMAGES.length
//								+ ImageName.HOOFS_IMAGES.length
//								+ ImageName.TENDON_IMAGES.length + ImageName.HEAD_IMAGES.length)) {
//
//							count = 0;
//							// unlockedLevels[10] = 13;
//							// unlockedLevels[11] = 14;
//							// unlockedLevels[12] = 15;
//
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 13, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 14, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 15, true);
//
//							new Achievement(
//									Constants.CHIROPRACTOR_ACHIEVEMENT_ID)
//									.unlock(new Achievement.UnlockCB() {
//
//										@Override
//										public void onSuccess(boolean newUnlock) {
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_OK);
//											// LearningBeforeActivity.this.finish();
//
//										}
//
//										@Override
//										public void onFailure(
//												String exceptionMessage) {
//											Toast.makeText(
//													PurchaseActivity.this,
//													"Error ("
//															+ exceptionMessage
//															+ ") unlocking achievement.",
//													Toast.LENGTH_SHORT).show();
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_CANCELED);
//											// LearningBeforeActivity.this.finish();
//										}
//
//									});
//
//							IMAGE_DIRECTORY = Constants.ORGANS_DIRECTORY;
//							imageFileName = ImageName.ORGANS_IMAGES;
//							BASE_URL = Constants.ORGANS_STANDARD_URL;
//
//						}
//
//						if (imageIndex == totalFiles) {
//							Toast.makeText(PurchaseActivity.this,
//									"Download completed", Toast.LENGTH_SHORT)
//									.show();
//							count = 0;
//							// unlockedLevels[13] = 16;
//							// unlockedLevels[14] = 17;
//							// unlockedLevels[15] = 18;
//							// unlockedLevels[16] = 19;
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 16, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 17, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 18, true);
//							// Utils.putLevelUnlocked(PurchaseActivity.this,
//							// Constants.KEY_LEVEL_UNLOCKED + 19, true);
//							new Achievement(Constants.FARRIER_ACHIEVEMENT_ID)
//									.unlock(new Achievement.UnlockCB() {
//
//										@Override
//										public void onSuccess(boolean newUnlock) {
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_OK);
//											// LearningBeforeActivity.this.finish();
//
//										}
//
//										@Override
//										public void onFailure(
//												String exceptionMessage) {
//											Toast.makeText(
//													PurchaseActivity.this,
//													"Error ("
//															+ exceptionMessage
//															+ ") unlocking achievement.",
//													Toast.LENGTH_SHORT).show();
//											PurchaseActivity.this
//													.setResult(Activity.RESULT_CANCELED);
//											// LearningBeforeActivity.this.finish();
//										}
//
//									});
//
//						}
//						// for (int i = 0; i < unlockedLevels.length; i++) {
//						// Utils.putLevelUnlocked(PurchaseActivity.this,
//						// Constants.KEY_LEVEL_UNLOCKED
//						// + unlockedLevels[i], true);
//						// }
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//
//				@Override
//				protected String doInBackground(String... arg0) {
//					try {
//						while (imageIndex < totalFiles) {
//
//							String url = BASE_URL
//									+ "/"
//									+ Utils.getAndroidFormattedUrl(imageFileName[count]);
//							Log.i("DREG", "URL = " + BASE_URL + "/"
//									+ imageFileName[count]);
//							Downloader.downloadFile(IMAGE_DIRECTORY,
//									imageFileName[count], url,
//									PurchaseActivity.this);
//							imageIndex++;
//							publishProgress("");
//						}
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//					return null;
//				}
//
//				@Override
//				protected void onPostExecute(String unused) {
//					progressDialog.dismiss();
//					// for (int i = 0; i < unlockedLevels.length; i++) {
//					// if (Utils.getLevelUnlocked(PurchaseActivity.this,
//					// Constants.KEY_LEVEL_UNLOCKED
//					// + unlockedLevels[i])) {
//					// isLevelUnlocked[unlockedLevels[i]] = true;
//					// }
//					// }
//					// LearningBeforeActivity.learnView
//					// .setLockedLevel(isLevelUnlocked);
//					// LearningBeforeActivity.learnView.invalidate();
//				}
//			}.execute();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.buyButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				buyButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				buyButton.setAlpha(0xFF);
			}
			break;
		case R.id.bundleButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				bundleButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				bundleButton.setAlpha(0xFF);
			}
			break;
		case R.id.muscleButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				muscleButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				muscleButton.setAlpha(0xFF);
			}
			break;
		case R.id.hoofButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				hoofButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				hoofButton.setAlpha(0xFF);
			}
			break;
		case R.id.deepMucleButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				deepMuscleButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				deepMuscleButton.setAlpha(0xFF);
			}
			break;
		case R.id.headButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				headButton.setAlpha(0x77);
			} else if ((event.getAction() == MotionEvent.ACTION_UP)
					|| (event.getAction() == MotionEvent.ACTION_MOVE)) {
				headButton.setAlpha(0xFF);
			}
			break;
		case R.id.organButton:
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				organButton.setAlpha(0x77);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				organButton.setAlpha(0xFF);
			}
			break;
		default:
			break;
		}
		return false;
	}

}
